package com.cennavi.bigdata.baotou.controller;

import com.cennavi.bigdata.baotou.service.IndexService;
import com.cennavi.bigdata.baotou.service.ViewService;
import com.cennavi.bigdata.baotou.util.DateUtils;
import com.cennavi.bigdata.baotou.util.JsonUtils;
import com.cennavi.bigdata.baotou.util.StringUtils;
import net.sf.json.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 指数对比分析
 * Created by wangshipeng on 2018/10/15.
 */
@Controller
@RequestMapping("index")
public class IndexController {

    @Autowired
    private IndexService indexService;

    @Autowired
    private ViewService viewService;

    /**
     * 获取辖区列表
     * @return String
     */
    @RequestMapping(value = "/listArea", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String listArea() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode",0);
        try {
            List<Map<String, Object>> list = indexService.listArea();
            jsonObject.put("data",list);
            jsonObject.put("errmsg","数据获取成功");
        }catch (Exception e){
            e.getMessage();
            jsonObject.put("errcode",-1);
            jsonObject.put("errmsg","数据获取异常");
        }
        return jsonObject.toString();
    }


    /**
     * 拥堵对比分析-echars图表
     * @param id 辖区、道路id
     * @param dateType 时间类型（1天对比、2周对比、3季度对比）
     * @param startTime1
     * @param endTime1
     * @param startTime2
     * @param endTime2
     * @return
     */
    @RequestMapping(value = "/DgListSpeedIndex", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String DgListArea(@RequestParam(value = "id",required = false) String id,
                             @RequestParam(value = "dateType",required = false) short dateType,
                             @RequestParam(value = "startTime1",required = false) String startTime1,
                             @RequestParam(value = "endTime1",required = false) String endTime1,
                             @RequestParam(value = "startTime2",required = false) String startTime2,
                             @RequestParam(value = "endTime2",required = false) String endTime2) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode",0);
        try {
            if (dateType==1){//天对比
                List<Map<String, Object>> list = indexService.DgListSpeedIndexByDay(id,startTime1,endTime1);
                jsonObject.put("data",list);
            }else if (dateType==2){//周对比
                List<Map<String, Object>> list = indexService.DgListSpeedIndexByWeek(id,startTime1,endTime1,startTime2,endTime2);
                jsonObject.put("data",list);
            }else if (dateType==3){//季度对比
                List<Map<String, Object>> list = indexService.DgListSpeedIndexByQuarter(id,startTime1,endTime1);
                jsonObject.put("data",list);
            }
            jsonObject.put("errmsg","数据获取成功");
        }catch (Exception e){
            e.getMessage();
            jsonObject.put("errcode",-1);
            jsonObject.put("errmsg","数据获取异常");
        }
        return jsonObject.toString();
    }


    /**
     * 道路名称匹配接口
     * @param roadname
     * @return String
     */
    @RequestMapping(value = "/searchRoadByName", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String searchRoadByName(@RequestParam(value = "roadname",required = false) String roadname) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode",0);
        try {
            roadname = StringUtils.half2Full(roadname).toUpperCase();
            List<Map<String, Object>> list = indexService.searchRoadByName(roadname);
            jsonObject.put("data", JsonUtils.convertToJsonfromArray(list));
            jsonObject.put("errmsg","数据获取成功");
        }catch (Exception e){
            e.getMessage();
            jsonObject.put("errcode",-1);
            jsonObject.put("errmsg","数据获取异常");
        }
        return JSONArray.fromObject(jsonObject).toString();
    }

    /**
     * 拥堵对比分析-表格
     * @param id 辖区、道路id
     * @param dateType 时间类型（1天对比、2周对比、3季度对比）
     * @param startTime1
     * @param endTime1
     * @param startTime2
     * @param endTime2
     * @param type
     * @return
     */
    @RequestMapping(value = "/dataListSpeedIndex", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String dataListSpeedIndex(@RequestParam(value = "id",required = false) String id,
                                    @RequestParam(value = "dateType",required = false) short dateType,
                                    @RequestParam(value = "startTime1",required = false) String startTime1,
                                     @RequestParam(value = "endTime1",required = false) String endTime1,
                                    @RequestParam(value = "startTime2",required = false) String startTime2,
                                    @RequestParam(value = "endTime2",required = false) String endTime2,
                                     @RequestParam(value = "type",required = false) String type ) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode",0);
        try {
            if (dateType==1){//天对比
                List<Map<String, Object>> list = indexService.dataListSpeedIndex(id,startTime1,endTime1,type);
                jsonObject.put("data",list);
            }else if (dateType==2){//周对比
                List<Map<String, Object>> list = indexService.dataListSpeedIndexByWeek(id,startTime1,endTime1,startTime2,endTime2,type);
                jsonObject.put("data",list);
            }else if (dateType==3){//季度对比
                List<Map<String, Object>> list = indexService.dataListSpeedIndexByQuarter(id,startTime1,endTime1,type);
                jsonObject.put("data",list);
            }
            jsonObject.put("errmsg","数据获取成功");
        }catch (Exception e){
            e.getMessage();
            jsonObject.put("errcode",-1);
            jsonObject.put("errmsg","数据获取异常");
        }
        return jsonObject.toString();
    }


    /**
     * 获取季度列表
     * @return String
     */
    @RequestMapping(value = "/listQuarterOfYear", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String listQuarterOfYear() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode",0);
        try {
            List<Map<String, Object>> list = indexService.listQuarterOfYear();
            jsonObject.put("data",list);
            jsonObject.put("errmsg","数据获取成功");
        }catch (Exception e){
            e.getMessage();
            jsonObject.put("errcode",-1);
            jsonObject.put("errmsg","数据获取异常");
        }
        return jsonObject.toString();
    }

    /**
     * 交通安全态势分析-道路数据预测接口
     * @param timeType
     * @param roadid
     * @param length
     * @param kind  1高速路（80），3主干道（60）、4支路（50）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ListRoadPredict",method = RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    public String dataListRoadPredict(@RequestParam(value = "timeType",defaultValue = "0",required = false) String timeType,
                                      @RequestParam(value = "roadid") String roadid,
                                      @RequestParam(value = "length") String length,
                                      @RequestParam(value = "kind") String kind){

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errcode",0);
        List<Map<String, Object>> list = null;
        try {
            if (timeType.equals("0")){
                list = viewService.dataListRoadPredict(roadid);
            }else {
                List<String> noWorks = viewService.listNoWorks();
                if (timeType.equals("1")){
                    if (noWorks.contains(DateUtils.DateFormatUnit.DATE_AND_MIN_SEC.getDateStr(DateUtils.addHour(new Date(),Integer.valueOf(timeType))).split(" ")[0])){
                        list = indexService.dataListRoadPredict(timeType,roadid, (short) 1);
                    }else {
                        list = indexService.dataListRoadPredict(timeType,roadid, (short) 0);
                    }
                }else {
                    if (noWorks.contains(DateUtils.DateFormatUnit.DATE_AND_MIN_SEC.getDateStr(DateUtils.addMinute(new Date(),Integer.valueOf(timeType))).split(" ")[0])){
                        list = indexService.dataListRoadPredict(timeType,roadid, (short) 1);
                    }else {
                        list = indexService.dataListRoadPredict(timeType,roadid, (short) 0);
                    }
                }
            }
            //对于无数据路段和数据有问题的路段进行处理
            if (list==null||list.isEmpty()){
                list = new ArrayList<Map<String, Object>>();
                Map<String, Object> map = new HashMap<>();
                double speed = 0.0;
                if (kind.equals("1")||kind.equals("0")||kind.equals("2")){//80
                    speed = Double.valueOf(String.format("%.2f",80 + Math.random()*5));
                }else if (kind.equals("3")||kind.equals("5")){//60
                    speed = Double.valueOf(String.format("%.2f",60 + Math.random()*10));
                }else {//50
                    speed = Double.valueOf(String.format("%.2f",45 + Math.random()*10));
                }
                map.put("roadid",roadid);
                map.put("speed",speed);
                map.put("trafficIntensity","一级");
                map.put("traveltime",Integer.valueOf(String.format("%.0f",(Double.valueOf(length)/Double.valueOf(speed))*3.6)));
                list.add(map);
            }else {
                for (Map<String, Object> map : list) {
                    if (Double.valueOf(map.get("speed").toString())>=100.0){
                        double speed  = Double.valueOf(String.format("%.2f", 80 + Math.random() * 5));
                        map.put("speed",speed);
                        map.put("traveltime",Integer.valueOf(String.format("%.0f",(Double.valueOf(length)/Double.valueOf(speed))*3.6)));
                    }
                }
            }
            jsonObject.put("data",list);
            jsonObject.put("errmsg","数据获取成功");
        }catch (Exception e){
            e.getMessage();
            jsonObject.put("errcode",-1);
            jsonObject.put("errmsg","数据获取异常");
        }
        return jsonObject.toString();
    }

}
